﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;

namespace EvaluationObjectCalculator
{
    public class Comput
    {
        private List<double> _alPrice;
        private ArrayList _alCompany;
        /// <summary>
        /// 对投标单位和价格排序，以价格从大到小排序，
        /// </summary>
        /// <param name="alPrice"></param>
        /// <param name="alCompany"></param>
        public void Sort(List<double> alPrice,ArrayList alCompany)
        {
            _alPrice = alPrice;
            _alCompany = alCompany;
            for (int i = 0; i < alPrice.Count - 1; i++) //最多做R.Length-1趟排序 
            {
                bool exchange = false; //本趟排序开始前，交换标志应为假
                double temp1 = 0;
                string temp2;

                for (int j = 0; j < alPrice.Count - 1 - i; j++)
                {
                    if (Convert.ToDouble(alPrice[j + 1]) > Convert.ToDouble(alPrice[j])) //交换条件
                    {
                        //排列价格
                        temp1 = Convert.ToDouble(alPrice[j + 1]);
                        alPrice.RemoveAt(j + 1);
                        alPrice.Insert(j, temp1);
                        //排列投标单位
                        temp2 = alCompany[j + 1].ToString(); ;
                        alCompany.RemoveAt(j + 1);
                        alCompany.Insert(j, temp2);

                        exchange = true; //发生了交换，故将交换标志置为真 
                    }
                }
                if (!exchange) //本趟排序未发生交换，提前终止算法 
                {
                    break;
                }
            }
        }

        /// <summary>
        /// 计算评标价的算术平均值A1
        /// </summary>
        /// <param name="al">价格数组</param>
        /// <param name="start">去掉最高价的个数</param>
        /// <param name="end">去掉最低价的个数</param>
        /// <returns></returns>
        public double avgArrsyList(List<double> al,int start,int end)
        {
            double _sumal = 0;
            double _avg = 0;
            try
            {
                for (int i = start; i < al.Count - end; i++)
                {
                    _sumal += al[i];
                }
                _avg = _sumal / (al.Count - start - end);
                return _avg;
            }
            catch (Exception)
            {
                throw;
            }
            
        }

        /// <summary>
        /// 与基准价A1相比比率
        /// </summary>
        /// <param name="price">投标价</param>
        /// <param name="avgA1">计算评标价的算术平均值A1</param>
        /// <returns></returns>
        public double A1BiLv(double price,double avgA1)
        {
            try
            {
                return Math.Round(100 * (price - avgA1) / avgA1, 2);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return 0;
            }
            
        }

        /// <summary>
        /// 评标参与第二次计算报价
        /// </summary>
        /// <param name="price">投标价</param>
        /// <param name="jiZhunA1BiLv">与基准价A1相比比率</param>
        /// <returns></returns>
        public double PingBiaoJia2(double price,double jiZhunA1BiLv,out bool isIn_20_10)
        {
            if(jiZhunA1BiLv >= -20 && jiZhunA1BiLv < 10)
            {
                isIn_20_10 = true;
                return price;
            }
            isIn_20_10 = false;
            return 0;
        }

        /// <summary>
        /// 寻找最小值
        /// </summary>
        /// <param name="PingBiaoJia2"></param>
        /// <returns></returns>
        public double MinPingBiaoJia2(double PingBiaoJia2, double temp)
        {
            if(PingBiaoJia2>temp)
            {
                return PingBiaoJia2;
            }
            return 0;
        }

        /// <summary>
        /// 计算mn的值
        /// </summary>
        /// <param name="price">投标价</param>
        /// <param name="pingBiaojunJia3">报价第三次算术平均值：A3</param>
        /// <param name="n"></param>
        /// <param name="m"></param>
        public void ComputeMN(double price, double pingBiaojunJia3, TextBox mtb1, TextBox mtb2, TextBox ntb3, TextBox ntb4, out double m, out double n)
        {
            if (price >= pingBiaojunJia3)
            {
                m=Math.Max(GetMN(mtb1), GetMN(mtb2));
                n = 1;
            }
            else if (price < pingBiaojunJia3)
            {
                m = Math.Min(GetMN(mtb1), GetMN(mtb2));
                n = 1;
            }
            //else if (price >= pingBiaojunJia3)
            //{
            //    m = Math.Max(GetMN(mtb1), GetMN(mtb2));
            //    n = 1;               
            //}
            //else if (price < pingBiaojunJia3)
            //{
            //    m = Math.Min(GetMN(mtb1), GetMN(mtb2));
            //    n = 1;                
            //}
            else
            {
                n = 0;
                m = 0;
            }
        }

        /// <summary>
        /// 计算排名和与第一名的分差
        /// </summary>
        /// <param name="deFengSort">得分集合</param>
        /// <param name="dGV">DataGridView表</param>
        /// <param name="cloumn">得分列的位置</param>
        ///  <param name="rowCount">有效行数</param>
        public void Ranking(List<double> deFengSort,DataGridView dGV,TextBox tb, int cloumn, int rowCount,out int count)
        {
            int sameCount = 0; //相同价格的数量
            deFengSort.Sort();  
            deFengSort.Reverse();

            for (int i = 0; i < deFengSort.Count; i++)
            {
                if(i > 0 && deFengSort[i] == deFengSort[i - 1])
                {
                    sameCount++;
                }
                for (int j = 0; j < dGV.Rows.Count; j++)
                {
                    if (Convert.ToDouble(dGV.Rows[j].Cells[cloumn].Value) == deFengSort[i])
                    {                      
                        dGV.Rows[j].Cells[cloumn + 1].Value = i + 1-sameCount; //写入排名
                        dGV.Rows[j].Cells[cloumn + 2].Value = Math.Round(deFengSort[0] - deFengSort[i], 4); //写入分差
                        break;
                    }
                }
            }

            //写入相同的排名及分差
            for (int i = 0; i < deFengSort.Count; i++)
            {
                if (i>0 && dGV.Rows[i].Cells[cloumn].Value.ToString().Equals(dGV.Rows[i - 1].Cells[cloumn].Value.ToString()))
                {
                    dGV.Rows[i].Cells[cloumn + 1].Value = dGV.Rows[i - 1].Cells[cloumn + 1].Value; //写入排名
                    dGV.Rows[i].Cells[cloumn + 2].Value = dGV.Rows[i - 1].Cells[cloumn + 2].Value; //写入分差
                }
                dGV.Rows[i].Cells[cloumn + 4].Value =Math.Round(Convert.ToDouble(dGV.Rows[i].Cells[cloumn + 2].Value.ToString())*Convert.ToDouble(tb.Text)/100,4); //写入评估值
            }
            count = deFengSort.Count;
        }

        /// <summary>
        /// 判断数据是否为空
        /// </summary>
        /// <param name="dataGridView1"></param>
        /// <returns></returns>
        public static bool isNull(DataGridView dataGridView1)
        {
            if (dataGridView1.Rows[1].Cells[1].Value.ToString()=="")
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取mn的值
        /// </summary>
        /// <param name="tb">textBox</param>
        /// <returns></returns>
        public double GetMN(TextBox tb)
        {
            string str = tb.Text.ToString().TrimStart().TrimEnd();
            double result=0;
            if (double.TryParse(str,out result))
            {
                return result;
            }
            else
            {
                MessageBox.Show(tb.Text.ToString()+"输入错误", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return 0;
            }
        }
    }
}
